package com.itc.dispatch.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itc.common.core.domain.R;
import com.itc.common.security.utils.SecurityUtils;
import com.itc.dispatch.common.Constents;
import com.itc.dispatch.common.SnakeTool;
import com.itc.dispatch.domain.DisFlightSearchConfig;
import com.itc.dispatch.domain.DisFlightTableConfig;
import com.itc.dispatch.domain.FlightOperationMonitoring;
import com.itc.dispatch.domain.dto.DisFlightConfigDto;
import com.itc.dispatch.domain.vo.DisFlightConfigVo;
import com.itc.dispatch.service.DisFlightSearchConfigService;
import com.itc.dispatch.service.DisFlightTableConfigService;
import com.itc.dispatch.mapper.DisFlightTableConfigMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.Field;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;

/**
* @author zhangyh
* @description 针对表【dis_flight_table_config(航班运行监控列表显示字段配置)】的数据库操作Service实现
* @createDate 2024-06-05 10:08:07
*/
@Service
public class DisFlightTableConfigServiceImpl extends ServiceImpl<DisFlightTableConfigMapper, DisFlightTableConfig>
    implements DisFlightTableConfigService{

    @Autowired
    public DisFlightSearchConfigService disFlightSearchConfigService;
    @Autowired
    public FlightOperationMonitoringServiceImpl flightService;


    @Override
    @Transactional
    public R configUpdate(DisFlightConfigDto dto) {
        List<DisFlightSearchConfig> searchConfigList = dto.getSearchConfigList();
        List<DisFlightTableConfig> tableConfigList = dto.getTableConfigList();
        if(searchConfigList.size() == 0 || tableConfigList.size() == 0){
            return R.fail("配置信息不能为空");
        }

        LocalDateTime now = LocalDateTime.now();
        Long userId = SecurityUtils.getUserId();
        Integer isOpen = dto.getSaveType();

        if(isOpen.intValue() == 2){
            //将之前应用的置为个人
            disFlightSearchConfigService.update(
                    new LambdaUpdateWrapper<DisFlightSearchConfig>()
                        .set(DisFlightSearchConfig::getIsOpen,0)
                        .eq(DisFlightSearchConfig::getIsOpen,1)
            );
            this.update(
                    new LambdaUpdateWrapper<DisFlightTableConfig>()
                            .set(DisFlightTableConfig::getIsOpen,0)
                            .eq(DisFlightTableConfig::getIsOpen,1)
            );
        }

        //删除个人原始数据
        disFlightSearchConfigService.getBaseMapper().delete(
                new LambdaQueryWrapper<DisFlightSearchConfig>()
                    .eq(DisFlightSearchConfig::getUserId,userId)
        );
        List<DisFlightSearchConfig> insertSearchList = new LinkedList<>();
        for (int i = 0; i < searchConfigList.size(); i++) {
            DisFlightSearchConfig searchConfig = searchConfigList.get(i);
            searchConfig.setSort(i);
            searchConfig.setIsOpen(isOpen);
            searchConfig.setUserId(userId);
            searchConfig.setUpdatedDate(now.toString());
            insertSearchList.add(searchConfig);
        }
        if(insertSearchList.size() > 0){
            disFlightSearchConfigService.saveBatch(insertSearchList);
        }

        //删除个人原始数据
        this.getBaseMapper().delete(
                new LambdaQueryWrapper<DisFlightTableConfig>()
                        .eq(DisFlightTableConfig::getUserId,userId)
        );
        List<DisFlightTableConfig> insertTableList = new LinkedList<>();
        for (int i = 0; i < tableConfigList.size(); i++) {
            DisFlightTableConfig tableConfig = tableConfigList.get(i);
            tableConfig.setSort(i);
            tableConfig.setIsOpen(isOpen);
            tableConfig.setUserId(userId);
            tableConfig.setUpdatedDate(now.toString());
            insertTableList.add(tableConfig);
        }
        if(insertTableList.size() > 0){
            this.saveBatch(insertTableList);
        }

        return R.ok();
    }
    @Override
    public R<DisFlightConfigVo> getCurrentUserConfigDetail() {
        DisFlightConfigVo resultData = new DisFlightConfigVo();
        List<DisFlightSearchConfig> searchConfigList = disFlightSearchConfigService.getBaseMapper().selectList(
                new LambdaQueryWrapper<DisFlightSearchConfig>()
                    .eq(DisFlightSearchConfig::getUserId,SecurityUtils.getUserId())
                    .orderByAsc(DisFlightSearchConfig::getSort)
        );
        List<DisFlightTableConfig> tableConfigList = this.baseMapper.selectList(
                new LambdaQueryWrapper<DisFlightTableConfig>()
                        .eq(DisFlightTableConfig::getUserId,SecurityUtils.getUserId())
                        .orderByAsc(DisFlightTableConfig::getSort)
        );
        //根据时间展示列表
        LambdaQueryWrapper<DisFlightSearchConfig> searchConfigLambdaQueryWrapper = new LambdaQueryWrapper<>();
        searchConfigLambdaQueryWrapper.eq(DisFlightSearchConfig::getIsOpen,Constents.Int_1);
        List<DisFlightSearchConfig> searchList = disFlightSearchConfigService.list(searchConfigLambdaQueryWrapper);

        LambdaQueryWrapper<DisFlightTableConfig> tableConfigLambdaQueryWrapper = new LambdaQueryWrapper<>();
        tableConfigLambdaQueryWrapper.eq(DisFlightTableConfig::getIsOpen,Constents.Int_1);
        List<DisFlightTableConfig> tableList = this.baseMapper.selectList(tableConfigLambdaQueryWrapper);

        if (searchList.size() != Constents.Int_0) {
            if (searchConfigList.get(0).getInsertDate().compareTo(searchList.get(0).getInsertDate()) > 0) {
                resultData.setSearchConfigList(searchConfigList);
            }
        }
        if (tableConfigList.size() != Constents.Int_0){
            if (tableConfigList.get(0).getInsertDate().compareTo(tableList.get(0).getInsertDate()) > 0) {
                resultData.setTableConfigList(tableConfigList);
            }
        }
        return R.ok(resultData);
    }

    @Override
    public R<DisFlightConfigVo> getApplyConfigDetail() {
        DisFlightConfigVo resultData = new DisFlightConfigVo();
        List<DisFlightSearchConfig> searchConfigList = disFlightSearchConfigService.getBaseMapper().selectList(
                new LambdaQueryWrapper<DisFlightSearchConfig>()
                        .eq(DisFlightSearchConfig::getUserId,SecurityUtils.getUserId())
                        .eq(DisFlightSearchConfig::getIsOpen,0)
                        .orderByAsc(DisFlightSearchConfig::getSort)
        );
        if(searchConfigList.size() == 0){
            searchConfigList = disFlightSearchConfigService.getBaseMapper().selectList(
                    new LambdaQueryWrapper<DisFlightSearchConfig>()
                            .eq(DisFlightSearchConfig::getIsOpen,1)
                            .eq(DisFlightSearchConfig::getUserId,SecurityUtils.getUserId())
                            .orderByAsc(DisFlightSearchConfig::getSort)
            );
        }
        List<DisFlightTableConfig> tableConfigList = this.baseMapper.selectList(
                new LambdaQueryWrapper<DisFlightTableConfig>()
                        .eq(DisFlightTableConfig::getUserId,SecurityUtils.getUserId())
                        .eq(DisFlightTableConfig::getIsOpen,0)
                        .orderByAsc(DisFlightTableConfig::getSort)
        );
        if(tableConfigList.size() == 0){
            tableConfigList = this.baseMapper.selectList(
                    new LambdaQueryWrapper<DisFlightTableConfig>()
                            .eq(DisFlightTableConfig::getUserId,SecurityUtils.getUserId())
                            .eq(DisFlightTableConfig::getIsOpen,1)
                            .orderByAsc(DisFlightTableConfig::getSort)
            );
        }
        resultData.setTableConfigList(tableConfigList);
        resultData.setSearchConfigList(searchConfigList);
        return R.ok(resultData);
    }


    //保存配置界面
    @Override
    @Transactional
    public R saveConfig(DisFlightConfigVo dto)  {
        LocalDateTime insertTime = LocalDateTime.now(); // 获取当前时间
        List<DisFlightTableConfig> tableConfigList = dto.getTableConfigList();
        List<DisFlightSearchConfig> searchConfigList = dto.getSearchConfigList();

        //清空当前用户展示列表选择配置
        LambdaQueryWrapper<DisFlightTableConfig> tableConfigLambdaQueryWrapper = new LambdaQueryWrapper<>();
        tableConfigLambdaQueryWrapper.eq(DisFlightTableConfig::getUserId,SecurityUtils.getUserId());
        tableConfigLambdaQueryWrapper.eq(dto.getType().intValue() == 0,DisFlightTableConfig::getIsOpen,0);
        this.baseMapper.delete(tableConfigLambdaQueryWrapper);
        //清空当前用户自定义筛选框配置
        LambdaQueryWrapper<DisFlightSearchConfig> configLambdaQueryWrapper = new LambdaQueryWrapper<>();
        configLambdaQueryWrapper.eq(DisFlightSearchConfig::getUserId,SecurityUtils.getUserId());
        configLambdaQueryWrapper.eq(dto.getType().intValue() == 0,DisFlightSearchConfig::getIsOpen,0);
        disFlightSearchConfigService.remove(configLambdaQueryWrapper);

        //列表配置
            if (tableConfigList != null && tableConfigList.size() != Constents.Int_0){
                    for (DisFlightTableConfig disFlightTableConfig : tableConfigList) {
                        //若为保存信息
                        if (dto.getType().intValue() == Constents.Int_0){
                            disFlightTableConfig.setIsOpen(0);
                        }else {
                        //若为应用信息
                            disFlightTableConfig.setIsOpen(1);
                        }
                        disFlightTableConfig.setUserId(SecurityUtils.getUserId());
                        disFlightTableConfig.setInsertDate(insertTime.toString());
                        this.baseMapper.insert(disFlightTableConfig);
                    }
            }
            if (searchConfigList != null && searchConfigList.size() != 0){
                //自定义筛选框配置
                    for (DisFlightSearchConfig disFlightSearchConfig : searchConfigList) {
                        if (dto.getType().intValue() == Constents.Int_0){
                            //保存自定义筛选框信息
                                disFlightSearchConfig.setIsOpen(0);
                        }else {
                            //应用自定义筛选框信息
                            disFlightSearchConfig.setIsOpen(1);
                        }
                            disFlightSearchConfig.setUserId(SecurityUtils.getUserId());
                            disFlightSearchConfig.setInsertDate(insertTime.toString());
                            disFlightSearchConfigService.save(disFlightSearchConfig);
                    }
            }
        return R.ok("保存/应用成功");
        }
}




